﻿using SharpAdbClient;
using SharpAdbClient.DeviceCommands;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DroidLord.Core
{
    class InputMethodManager
    {
        public void  InstallApk(DeviceData device,String apkPath)
        {
            AdbManager.InstallApk(device, apkPath);
        }
        public Boolean  IsInstalled(DeviceData deviceData,String pkgName)
        {
            Boolean retB = false;
            List<string> apkList = AdbManager.GetInstalledPackages(deviceData);
            foreach(string mypkgname in apkList)
            {
                if(mypkgname.Equals(pkgName))
                {
                    retB = true;
                    break;
                }
            }
            return retB;
        }
        public  void  enableIme(DeviceData deviceData,String imeString)
        {
            string cmd1 = "ime enable " + imeString;
            string cmd2 = "ime set " + imeString;
            AdbManager.executeShell(deviceData, cmd1);
            AdbManager.executeShell(deviceData, cmd2);

        }


        public  Boolean isNeedUpdate(DeviceData deviceData,string pkgName)
        {
            string phoneVersionName = getInstalledVersionName(deviceData, pkgName);
            string pcVersinName = getPcVersionName(deviceData);
            if(phoneVersionName.Equals("")||pcVersinName.Equals(""))
            {
                return false;
            }
            if(phoneVersionName.Equals(pcVersinName))
            {
                return false;
            }
            return true;
        }

        public string  getInstalledVersionName(DeviceData deviceData,string pkgNmae)
        {
            return AdbManager.getAppVersionName(deviceData, pkgNmae).Trim();
        }
        /// <summary>
        /// 获取pc端 输入法apk版本
        /// </summary>
        /// <param name="deviceData"></param>
        /// <param name="pkgNmae"></param>
        /// <returns></returns>
        public string getPcVersionName(DeviceData deviceData)
        {
            string apkapth = PathManager.getInputMethodApkPath();
            if (File.Exists(apkapth))
            {
                FileInfo fi = new FileInfo(apkapth);
                string apkName = fi.Name;
                return apkName.Replace("inputmethod_", "").Replace(".apk", "").Trim();
            }
            return "";
        }


        public void clearText(DeviceData deviceData)
        {
            //ADB_CLEAR_TEXT
            string cmd = "am broadcast -a ADB_CLEAR_TEXT";
            //MessageBox.Show(cmd);
            AdbManager.executeShellNoRead(deviceData, cmd);
        }

        public void  inputText(DeviceData deviceData,String text)
        {
            byte[] b = System.Text.Encoding.UTF8.GetBytes(text);
            //转成 Base64 形式的 System.String  
            
            string mytext = Convert.ToBase64String(b);
            string cmd = "am broadcast -a ADB_INPUT_TEXT --es text "+mytext;
            //MessageBox.Show(cmd);
            AdbManager.executeShellNoRead(deviceData,cmd);
           //AdbManager.adbShellNoRead(deviceData, cmd);
           // MessageBox.Show(retMsg);
        }

        public void  copyTextToPhone(DeviceData deviceData,String text)
        {
            //ADB_COPY_TEXT
            byte[] b = System.Text.Encoding.UTF8.GetBytes(text);
            //转成 Base64 形式的 System.String  
            string mytext = Convert.ToBase64String(b);
            string cmd = "am broadcast -a ADB_COPY_TEXT --es text " + mytext;
            AdbManager.executeShellNoRead(deviceData, cmd);
        }
        //ADB_PAST_TEXT
        public void pasteTextToPc(DeviceData deviceData)
        {
            //ADB_COPY_TEXT
           
            //转成 Base64 形式的 System.String  
            string cmd = "am broadcast -a ADB_PAST_TEXT";
            string retmsg=AdbManager.executeShell(deviceData, cmd);
            //MessageBox.Show(retmsg);
        }
    }
}
